#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <pthread.h>

#define MAX_BUFFER 128
#define DAYTIME_SERVER_PORT 1300


void* serv(void* pcsock){
	int csock=(int)pcsock;
	write(csock,"note this",9);
	sleep(5);
	char* buffer=malloc(256);
	bzero(buffer,256);
	time_t ct=time(NULL);
	snprintf(buffer,256,"\n%x,%s\n",pthread_self(),ctime(&ct));
	write(csock,buffer,strlen(buffer));
	close(csock);
	pthread_exit(NULL);
}

int main(int argc,char** argv){
	int sock,csock;
	struct sockaddr_in servaddr;
	char timebuffer[MAX_BUFFER+1];
	time_t ct;

	sock=socket(AF_INET,SOCK_STREAM,0);
	memset(&servaddr,0,sizeof(servaddr));
	servaddr.sin_family=AF_INET;
//	servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
	servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
//	printf("%c ,%s\n",servaddr.sin_addr.s_addr,
	servaddr.sin_port=htons(DAYTIME_SERVER_PORT);
	int tmp;
	setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&tmp,sizeof(tmp));

	int ret=bind(sock,(struct sockaddr*)&servaddr,sizeof(servaddr));
	if(ret<0){
		printf("bind error return %d\n",ret);
		exit(-1);
	}

	listen(sock,5);

	while(1){
		csock=accept(sock,(struct sockaddr*)NULL,NULL);
		if(csock>=0){
			pthread_t th;
			ret=pthread_create(&th,NULL,serv,(void*)csock);
		}
	}
}
